#include "Component.h"
#include "Transform.h"
#include "Renderer.h"
#include "GameObject.h"
#include <iostream>
namespace ManchyGL
{
	GameObject::GameObject()
	{
		_name = "";
		_keyToComponent = new map<int, Component*>();
	}

	GameObject::GameObject(const char* name)
	{
		_name = name;
		_keyToComponent = new map<int, Component*>();
	}

	GameObject::~GameObject()
	{
		if (NULL != _keyToComponent)
		{
			map<int, Component*>::iterator iter = _keyToComponent->begin();
			while (iter != _keyToComponent->end()) {
				delete iter->second;
				iter++;
			}
			delete _keyToComponent;
			_keyToComponent = NULL;
		}
	}

	void GameObject::SetName(const char* name)
	{
		_name = name;
	}

	const char* GameObject::GetName()
	{
		return _name;
	}

	bool GameObject::CompareName(const char* name)
	{
		return false;
	}

	void GameObject::AddComponent(int componentType, Component* component)
	{
		if (NULL != component->GetGameObject())
			return;

		(*_keyToComponent)[componentType] = component;
		component->SetGameObject(this);
	}

	Component* GameObject::GetComponent(int componentType)
	{
		Component* com = (*_keyToComponent)[componentType];
		return com;
	}

	Transform* GameObject::GetTransform()
	{
		void* component = this->GetComponent(ComponentType_Transform);
		return (Transform*)component;
	}

	Renderer* GameObject::GetRenderer()
	{
		void* component = this->GetComponent(ComponentType_Renderer);
		return (Renderer*)component;
	}

	Camera* GameObject::GetCamera()
	{
		void* component = this->GetComponent(ComponentType_Renderer);
		return (Camera*)component;
	}

}